package com.example.administrator.listview_test;

import android.content.Context;
import android.support.annotation.*;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class PhotoAdapter extends ArrayAdapter<Photo> {
    private int resourceId;

    public PhotoAdapter(Context context, int textViewResourceId, List<Photo> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("debug:", "converView:" +convertView);
        Photo photo = getItem(position);//获取photo实例
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) view.findViewById(R.id.photo_image_test);
            viewHolder.text = (TextView) view.findViewById(R.id.photo_name_test);
            view.setTag(viewHolder);//保存view中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.image.setImageResource(photo.getImage());
        viewHolder.text.setText(photo.getName());
        return view;
    }

    class ViewHolder {
        ImageView image;
        TextView text;
    }

}
